package org.hlopik.riverGuide.client.view;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import org.hlopik.riverGuide.client.ApplicationFacade;
import org.hlopik.riverGuide.client.model.dto.RiverGuideDTO;
import org.puremvc.java.multicore.interfaces.INotification;
import org.puremvc.java.multicore.patterns.mediator.Mediator;

/**
 *
 * @author f
 */
public class AddRiverGuideFormMediator extends Mediator {

    public static final String NAME = "AddRiverGuideFormMediator";
    private RiverGuideDTO riverGuideDTO;
    private Button addRiverGuide = new Button("Add River Guide");
    private Label nameLabel = new Label("Name: ");
    private TextBox nameTextBox = new TextBox();
    private Label descriptionLabel = new Label("Description: ");
    private TextBox descriptionTextBox = new TextBox();
    private VerticalPanel verticalPanel = new VerticalPanel();
    private HorizontalPanel nameHPanel = new HorizontalPanel();
    private HorizontalPanel descriptionHPanel = new HorizontalPanel();
    private DialogBox dialogBox;

    public AddRiverGuideFormMediator() {
        super(NAME, null);
        initView();
    }

    private void initView() {        
        nameHPanel.add(nameLabel);
        nameHPanel.add(nameTextBox);
        descriptionHPanel.add(descriptionLabel);
        descriptionHPanel.add(descriptionTextBox);
        verticalPanel.add(nameHPanel);
        verticalPanel.add(descriptionHPanel);
        verticalPanel.add(addRiverGuide);

        addRiverGuide.addClickHandler(new ClickHandler() {

            public void onClick(ClickEvent event) {
                onAddRiverGuide();
            }
        });
        this.setViewComponent(verticalPanel);
    }

    private void onAddRiverGuide() {
        riverGuideDTO = new RiverGuideDTO();
        riverGuideDTO.setName(nameTextBox.getText());
        riverGuideDTO.setDescription(descriptionTextBox.getText());
        sendNotification(ApplicationFacade.ADD_RIVER_GUIDE, riverGuideDTO);
        clearForm();
    }

    private void clearForm() {
        nameTextBox.setText("");
        descriptionTextBox.setText("");
    }

    @Override
    public String[] listNotificationInterests() {
        return new String[]{ApplicationFacade.ADD_RIVER_GUIDE_FAULT,
                    ApplicationFacade.ADD_RIVER_GUIDE_SUCCESS};
    }

    @Override
    public void handleNotification(INotification notification) {

        String name = notification.getName();
        if (ApplicationFacade.ADD_RIVER_GUIDE_FAULT.equals(name)) {
            Window.alert("AddRiverGuide failed: " + notification.getBody());
        } else if (ApplicationFacade.ADD_RIVER_GUIDE_SUCCESS.equals(name)) {
            //Window.alert("OK");
        }
        super.handleNotification(notification);
    }
}
